package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.activity.MyOrderDetailActivity;
import com.xindaoapp.happypet.activity.MyOrderListActivity;
import com.xindaoapp.happypet.bean.MyOrder;
import com.xindaoapp.happypet.bean.ShoppingCartInfo;
import com.xindaoapp.happypet.utils.CommonUtil;

public class MyOrderAdapter extends XinDaoBaseAdapter<MyOrder> {
    private final String flag;
    private final Context mContext;
    private int i = -1;

    public MyOrderAdapter(Context context, List<MyOrder> iniData, int pageSize, int res,
            int loadingRes, String flag) {
        super(context, iniData, pageSize, res, loadingRes);
        this.flag = flag;
        this.mContext = context;
    }

    public int getClickChild() {
        return i;
    }

    public void setClickChild(int i) {
        this.i = i;
    }

    @Override
    public void nextPage(
            int start,
            int size,
            com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<MyOrder> iLoadNextPageData) {
        MyOrderListActivity.getData(mContext, flag, start, size, iLoadNextPageData);

    }

    private static class ViewHolder {
        TextView id;
        TextView price;
        TextView time;
        LinearLayout ll;
        TextView state;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent, final MyOrder order) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (null == holder) {
            holder = new ViewHolder();
            holder.id = (TextView) convertView.findViewById(R.id.order_id);
            holder.price = (TextView) convertView.findViewById(R.id.order_price);
            holder.time = (TextView) convertView.findViewById(R.id.order_time);
            holder.ll = (LinearLayout) convertView.findViewById(R.id.hsv);
            holder.state = (TextView) convertView.findViewById(R.id.order_state);

            convertView.setTag(holder);
        }

        holder.id.setText("订单号:" + order.order_info.order_sn);
        holder.id.setText(changeTextColor(holder.id, 4, order.order_info.order_sn.length(),
                "#666666"));
        holder.price.setText("订单金额:" + "￥"
                + CommonUtil.transitionToFloat(order.order_info.order_amount));
        holder.price.setText(changeTextColor(holder.price, 5,
                String.valueOf(CommonUtil.transitionToFloat(order.order_info.order_amount))
                        .length() + 1, "#DD5C3C"));
        holder.time.setText("下单时间:" + order.order_info.add_time);
        holder.time.setText(changeTextColor(holder.time, 5, order.order_info.add_time.length(),
                "#666666"));

        /*
         * String pay_status = order.order_info.pay_status; String shipping_status =
         * order.order_info.shipping_status; String order_status = order.order_info.order_status; if
         * (pay_status.equals("2") && shipping_status.equals("2") && order_status.equals("1")) {
         * holder.state.setText("订单状态:" + "待评价"); holder.state.setText(changeTextColor(holder.state,
         * 5, 3, "#DD5C3C")); } else if (pay_status.equals("2") && shipping_status.equals("2") &&
         * order_status.equals("4")) { holder.state.setText("订单状态:" + "正在退货");
         * holder.state.setText(changeTextColor(holder.state, 5, 4, "#DD5C3C")); } else if
         * (pay_status.equals("2") && shipping_status.equals("0")) { holder.state.setText("订单状态:" +
         * "待发货"); holder.state.setText(changeTextColor(holder.state, 5, 3, "#DD5C3C")); } else if
         * (pay_status.equals("0") && !order_status.equals("3") && !order_status.equals("2")) {
         * holder.state.setText("订单状态:" + "待付款"); holder.state.setText(changeTextColor(holder.state,
         * 5, 3, "#DD5C3C")); } else if (pay_status.equals("2") && (shipping_status.equals("4") ||
         * shipping_status.equals("1"))) { holder.state.setText("订单状态:" + "待收货");
         * holder.state.setText(changeTextColor(holder.state, 5, 3, "#DD5C3C")); }
         */

        switch (order.order_info.type_status) {
            case 1:
                holder.state.setText("订单状态:" + "待付款");
                holder.state.setText(changeTextColor(holder.state, 5, 3, "#DD5C3C"));
                break;
            case 2:
                holder.state.setText("订单状态:" + "待发货");
                holder.state.setText(changeTextColor(holder.state, 5, 3, "#DD5C3C"));
                break;
            case 3:
                holder.state.setText("订单状态:" + "待收货");
                holder.state.setText(changeTextColor(holder.state, 5, 3, "#DD5C3C"));
                break;
            case 4:
                holder.state.setText("订单状态:" + "已完成");
                holder.state.setText(changeTextColor(holder.state, 5, 3, "#DD5C3C"));
                break;

            default:
                break;
        }

        holder.ll.removeAllViews();
        for (int i = 0; i < order.goods_info.size(); i++) {
            ImageView iv = new ImageView(mContext);
            iv.setPadding(1, 1, 1, 1);
            iv.setBackgroundColor(Color.parseColor("#D1D0D4"));
            ImageLoader.getInstance().displayImage(order.goods_info.get(i).goods_thumb, iv);
            LayoutParams lp = new LayoutParams(100, 100);
            lp.leftMargin = 10;
            iv.setLayoutParams(lp);
            holder.ll.addView(iv);
        }
        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MyOrderDetailActivity.class);
                setClickChild(position);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("orderID", order.order_info.order_id);
                int count = 0;
                for (ShoppingCartInfo info : order.goods_info) {
                    if (info.is_commented == 0)
                        count++;
                }
                intent.putExtra("is_commented", count > 0 ? 0 : 1);
                intent.putExtra("time", order.order_info.add_time);
                intent.putExtra("flag", flag);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    private SpannableStringBuilder changeTextColor(TextView tv, int start, int end, String color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(tv.getText().toString());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.parseColor(color));
        builder.setSpan(redSpan, start, start + end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }
}
